﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TY.Webs.ClassLib;
using JCW.Common;
using Leefish.Market.PRO.BLL;
using Leefish.Market.PRO.Entity;
using System.Collections.Generic;
using Leefish.Market.PRO;
using System.Text.RegularExpressions;

namespace Leefish.Market.Web.Handler.Admin
{
    /// <summary>
    ///AdminHandle 的摘要说明
    /// </summary>
    public class AdminHandler:CommonPageBase
    {
        private BzWinBLL bzBLL;
        public AdminHandler()
        {
            
        }

        public override void ProcessRequest(HttpContext context){
            //base.ProcessRequest(context);
            //context.Response.Write("aaaaaaaaaa");
            Transfers tf = new Transfers();
            switch (action) {
                case "loginCheck": tf = CheckValidCode(context); break;     //验证用户登录

                case "addRoller": tf = AddRoller(context); break;//添加修改投资基本信息
                case "setGift": tf = SetGift(context); break;
            }
            context.Response.Write(tf.ToJson());
            context.Response.End();
        }

        private Transfers SetGift(HttpContext context)
        {
            Transfers tf = new Transfers();
            string input = requestUtility.GetFormString("data");
            Regex r = new Regex("~~");
            Regex r2 = new Regex("@@");
            Regex r3 = new Regex("##");
            int rel = 0;
            string[] list = r.Split(input);
            
            List<EnAward> enList = new List<EnAward>();
            EnRoller roller = BzRoller.Get(Client.UserNick);
            if (roller != null)
            {
                EnAward en;
                string[] e;
                try
                {
                    foreach (string a in list)
                    {
                        en = new EnAward();
                        e = r2.Split(a);
                        en.ATypeID = ConvertHelper.ToInt(r3.Split(e[0])[0]);
                        en.ATypeName = r3.Split(e[0])[1];
                        en.AName = e[1];
                        en.AllNum = ConvertHelper.ToInt(e[2]);
                        en.Probability = ConvertHelper.ToDecimal(e[3]);
                        en.Nick = Client.UserNick;
                        en.Num = 0;
                        en.RollerID = roller.ID;
                        en.AddDate = DateTime.Now;
                        enList.Add(en);
                    }

                    //添加
                    BzAward.DeleteAwards(string.Format(" Nick='{0}'", Client.UserNick));
                    foreach (EnAward enAward in enList) {
                        BzAward.Insert(enAward);
                    }
                    rel = 1;
                }
                catch (Exception ex) {
                    rel = -1;
                }
            }
            tf.AddColumn("rel", rel);
            //BzAward.DeleteAwards(string.Format(" Nick='{0}'", Client.UserNick));
            //IList<EnAward> list = BzAward.GetListByfilter(string.Format(" Nick='{0}'", Client.UserNick), 1, 10);

            return tf;
        }

        private Transfers AddRoller(HttpContext context)
        {
            Transfers tf = new Transfers();
            int rel = 0;
            EnRoller roller = BzRoller.Get(Client.UserNick);
            if (roller == null) { roller = new EnRoller(); }
            roller.PName = requestUtility.GetFormString("aName");
            roller.BeginTime = requestUtility.GetDateTime("beginDate");
            roller.EndTime = requestUtility.GetDateTime("endDate");
            roller.Share = requestUtility.GetFormString("share");
            roller.Notice = requestUtility.GetFormString("notice");
            roller.Probability = ConvertHelper.ToDecimal(requestUtility.GetFormString("pro"));
            roller.Nick = Client.UserNick;
            roller.Session = Client.Session;
            roller.AddPer = Client.UserNick;
            roller.AName = roller.PName;
            roller.AddTime = DateTime.Now;
            roller.IsOpen = 0;
            if (roller.ID == 0)
            {
                rel = BzRoller.Insert(roller);
            }
            else {
                rel = BzRoller.Update(roller);
            }
            tf.AddColumn("rel", rel);
            return tf;
        }

        
        private Transfers CheckValidCode(HttpContext context)
        {
            Transfers tf = new Transfers();
            int result = 0;
            string msg = "";
            string code = requestUtility.GetFormString("code");
            try
            {
                if (context.Session["Identify"].ToString().ToLower() == code.ToString().ToLower())
                {
                    result = 1;
                }
                else
                {
                    msg = "wrong volidcode";
                }
            }
            catch (Exception ex)
            { 
                
            }
            tf.AddColumn("result", result);
            tf.AddColumn("msg", msg);
            return tf;
        }

    }
}